<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { ElTable, ElMessageBox, FormInstance, type FormRules, ElMessage } from "element-plus"
// 接口
import { addDyLive, putDyLive, delDyLive, getDyLive } from "@/api/dyLive"
import { type getTeamData } from "@/api/team/types/team"
import { usePagination } from "@/hooks/usePagination"
import { Search, Refresh } from "@element-plus/icons-vue"
const loading = ref(true)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
defineOptions({
  name: "DyLive"
})

// #region 改
const currentId = ref<undefined | string>(undefined)
const handleUpdate = (row: any) => {
  currentId.value = row.dyId
  formData.dyName = row.dyName
  formData.dyId = row.dyId
  dialogVisible.value = true
}
// #endregion

// #region  删
const handleDelete = (row: any) => {
  ElMessageBox.confirm(`您确定要删除:${row.dyName}吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    delDyLive(row.dyId).then(() => {
      ElMessage.success("删除成功")
      getDy()
    })
  })
}
// #endregion

// #region 增
const formRef = ref<FormInstance | null>(null)
const formData = reactive<any>({
  dyName: "",
  dyId: ""
})
const dialogVisible = ref<boolean>(false)
const rules: FormRules = reactive({
  dyName: [{ required: true, trigger: "blur", message: "请输入名称" }],
  dyId: [{ required: true, trigger: "blur", message: "请输入名称" }]
})
const handleCreate = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      if (currentId.value === undefined) {
        addDyLive({
          dyName: formData.dyName,
          dyId: formData.dyId
        }).then(() => {
          ElMessage.success("新增成功")
          dialogVisible.value = false
          getDy()
        })
      } else {
        putDyLive({
          id: currentId.value,
          dyName: formData.dyName,
          dyId: formData.dyId
        }).then(() => {
          ElMessage.success("修改成功")
          dialogVisible.value = false
          getDy()
        })
      }
    } else {
      return false
    }
  })
}

const resetForm = () => {
  currentId.value = undefined
  clearValidate()
  formData.dyName = ""
  formData.dyId = ""
}
// #endregion

// #region 查
const tableTotal = ref<number>()
const tableData = ref<getTeamData[]>([])
const getDy = () => {
  getDyLive({
    page: paginationData.currentPage,
    size: paginationData.pageSize,
    dyName: searchData.dyName
  })
    .then((res: any) => {
      tableData.value = res.data.list
      tableTotal.value = res.data.total
      loading.value = false
    })
    .catch(() => {
      tableData.value = []
    })
}
// #endregion

// #region 搜索内容
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive<any>({
  dyName: ""
})
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getDy()
    console.log("重置")
  }
  paginationData.currentPage = 1
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getDy()
    console.log("查询")
  }
  paginationData.currentPage = 1
}
// #endregion

/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getDy, { immediate: true })
const clearValidate = () => {
  formRef.value?.clearValidate()
}
const arr = ref([0, 1, 2, 3, 2, 3, 4, 7, 8, 8])
const arrA = ref()
// arrA.value = arr.value.map((item: any) => {
//   return item * 2
// })
arrA.value = arr.value.filter((item: any) => {
  return item > 4
})
console.log(arrA.value, "filter")
</script>

<template>
  <div class="app-container">
    <el-card shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="dyName" label="直播间">
          <el-input v-model.trim="searchData.dyName" clearable placeholder="请输入" style="width: 240px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 中间部分 -->
    <el-card v-loading="loading" shadow="never">
      <!-- 顶部添加职位 -->
      <div class="middle-top">
        <div>
          <el-button type="primary" @click="dialogVisible = true">添加</el-button>
        </div>
      </div>
      <!-- 中间渲染内容 -->
      <div class="table-center">
        <el-table :data="tableData">
          <el-table-column prop="dyName" label="直播间名称" align="center" />
          <el-table-column prop="dyId" label="直播ID" align="center" />
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :page-size="paginationData.pageSize"
          :total="tableTotal"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      :title="currentId === undefined ? '新增' : '修改'"
      @close="resetForm"
      width="30%"
    >
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px" label-position="left">
        <el-form-item prop="dyName" label="直播间名称">
          <el-input v-model="formData.dyName" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="dyId" label="直播间ID">
          <el-input v-model="formData.dyId" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
// .box-card {
//   height: 70px;
//   margin-bottom: 20px;
// }
.search-wrapper {
  margin-bottom: 20px;
}
.middle-top {
  margin-bottom: 20px;
}
.table-center {
  margin-bottom: 20px;
}
.pagination {
  display: flex;
  justify-content: flex-end;
}
</style>
